package com.coastline.fruit.usercenter.controller;

import com.coastline.fruit.common.exception.CoastLineFruitException;
import com.coastline.fruit.redis.util.RedisUtil;
import com.coastline.fruit.usercenter.entity.User;
import com.coastline.fruit.usercenter.exception.UserCenterErrorInfo;
import com.coastline.fruit.common.email.MailUtil;
import com.coastline.fruit.usercenter.request.CreateUserReq;
import com.coastline.fruit.usercenter.response.CreateUserRsp;
import com.coastline.fruit.usercenter.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理 入口
 *
 * @author Coastline
 * @date 2025/1/1 22:25
 */
@Api(tags = "userCenter-用户管理")
@RestController
@RequestMapping("/coastline/fruit/v1/user")
@Slf4j
@RefreshScope
public class UserController {

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private MailUtil mailUtil;

    @Value("${datasource.url}")
    private String nacosString;

    @Resource
    private TemplateEngine templateEngine;

    @Autowired
    private UserService userService;

    @Value("${spring.mail.username}")
    private String emailFrom;

    @ApiOperation(value = "新增用户")
    @PostMapping("/create")
    public CreateUserRsp createUser(@RequestBody CreateUserReq createUserReq) throws CoastLineFruitException {
        return userService.createUser(createUserReq);
    }

    @GetMapping("/send")
    public String sendEmail() throws MessagingException {
        Context context = new Context();
        Map<String,Object> map = new HashMap<>();
        map.put("username", "coastline");
        context.setVariables(map);
        String content = templateEngine.process("create", context);
        mailUtil.sendHtmlMail(emailFrom, "gyhhappyday@outlook.com", "欢迎使用xx", content);
        return "发送成功";
    }

    @GetMapping("/test")
    public String test() {
        return nacosString;
    }

    /**
     * 测试redis是否联通
     *
     * @return
     * @throws CoastLineFruitException
     */
    @GetMapping("/redis")
    public String testRedis() throws CoastLineFruitException {
        String value;
        try {
            User userVo = new User();
            userVo.setUserName("hello01");
            redisUtil.setStringValue("redis03", "hello2");
            value = redisUtil.getStringValue("redis03");
        } catch (Exception e) {
            log.error("exception", e);
            throw new CoastLineFruitException(UserCenterErrorInfo.DATABASE_OPERATION_ERROR);
        }
        return value;
    }
}
